<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Expert extends La_Controller {
	function __construct() {
		parent::__construct();
		$this->data = array('page_title' => '行动网');
		$this->load->model('Category_model');
		$this->load->model('City_model');
		$this->load->model("Expert_model");
	}
	
	function create() {
		$this->page_title("创建行家-行动网");
		if (!$this->authorize->is_logged_in()) {
			la_redirect('user/login', '创建行家请先登录');
		}
		$current_user = $this->authorize->current_user();
		$expert_count = $this->Expert_model->get_count_by_user_id($current_user);
		if ($expert_count >= 4) {
			la_redirect("user/show", "你的行家已达上限");
		}
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$this->_save();
		} else {
			$this->data['provinces'] = $this->City_model->find_all_province();
			$this->data['first_level_categories'] = $this->Category_model->find_son_category(1);
			$this->load->view('expert/create', $this->data);
		}
	}
	
	/**
	 * 编辑行家
	 */
	public function edit() {
		$this->page_title("编辑行家-行动网");
		$expert_id = intval($this->uri->segment(3));
		$expert = $this->Expert_model->find_by_id($expert_id);
		$this->data = array_merge($this->data, $expert);
		$this->data['provinces'] = $this->City_model->find_all_province();
		$this->data['first_level_categories'] = $this->Category_model->find_son_category(1);
		$this->data['second_level_category_id'] = $this->Category_model->find_father_category($expert['category_id']);
		
		$this->data['first_level_category_id'] = $this->Category_model->find_father_category($this->data['second_level_category_id']);
		
		if (!empty($expert['created_by']) && $expert['created_by'] == $this->authorize->current_user()) {
			if ($_SERVER['REQUEST_METHOD'] == 'POST') {
				$this->_save();
			} else {
				$this->load->view('expert/create', $this->data);
			}
		} else {
			la_rediect("", "你没有权利进行此操作");
		}
	}
	
	public function _save() {
		$title = $this->input->post('title');
		$id = $this->input->post('id');
		$price = $this->input->post('price');
		$introduction = $this->input->post('introduction');
		$description = $this->input->post('description');
		$category_id = $this->input->post('category_id');
		$province_id = $this->input->post('province');
		$city_id = $this->input->post('city');
		$area_id = $this->input->post('area');
		$data = array('title' => $title, 'price' => $price,'description' => $description,
					 'province_id' => $province_id, 'city_id' => $city_id, 'area_id' => $area_id,
					 'introduction' => $introduction, 'category_id'=> $category_id, 'created_by' => $this->authorize->current_user(), 'created' => date("Y-m-d H:i:s"));
		if (!empty($id)) {
			$data['id'] = $id;
		}
		$expert_count = $this->Expert_model->get_count_by_user_id($data['created_by']);
		if ($expert_count >= 4 && empty($id)) { //非新建
			la_redirect("user/show", "你的行家已达上限");
		}
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		$this->form_validation->set_rules('title', '行家名称', 'required');
		$this->form_validation->set_rules('price', '服务价格', 'required');
		if ($this->form_validation->run() == FALSE)
		{
			
			$data['provinces'] = $this->City_model->find_all_province();
			$this->load->view('expert/create', $data);
			
		} else {
			$this->data['expert_id'] = $this->Expert_model->add($data);
			$this->data['user_id'] = $data['created_by'];
			$this->data['leave_expert_count'] = 4 - 1 - $expert_count;
			if (empty($id)) {
				$this->load->view('expert/create_success', $this->data);
			} else {
				la_redirect("user/show?expert_id=". $id, "修改成功");
			}
		}
	}
	
	/**
	 * 行家列表
	 */
	public function lists() {
		$this->page_title("行家-行动网");
		$offset = intval($this->uri->segment(4));
		$this->data['provinces'] = $this->City_model->find_all_province();
		$category_id = $this->uri->segment(3);
		
		$this->data['expert_count'] = $this->Expert_model->get_count_by_condition(" e.category_id=" . intval($category_id));
		$this->load->library('pager');
		$config['base_url'] = site_url("expert/lists" . "/" . $category_id ) . "/";
		$config['total_rows'] = $this->data['expert_count'];
		$config['per_page'] = LA_PAGE_SIZE;
		$config['uri_segment'] = 4;
		$this->pager->initialize($config);
		$city_ids = array();
		$area_ids = array();
		$this->load->model(array("Comment_model", "User_model"));
		$experts = $this->Expert_model->find_by_condition(" e.category_id=" . intval($category_id), $offset, $config['per_page']);
		for($i = 0, $n = count($experts); $i < $n; $i++) {
			$city_ids[] = $experts[$i]['city_id'];
			$area_ids[] = $experts[$i]['area_id'];
		}
		if (!empty($experts)) {
			$expert_string = '-1';
			foreach($experts as &$expert) {
				$expert_string .= "," . $expert['created_by'];
				$expert['score'] = 0;
				$expert['employ_count'] = 0;
				$expert['vertification'] = '';
			}
			unset($expert);
			$scores = $this->Comment_model->find_average_score($expert_string, 'user');
			for($i = 0, $n = count($scores); $i < $n; $i++) {
				foreach($experts as &$expert) {
					if ($expert['created_by'] == $scores[$i]['target_id']) {
						$expert['score'] = $scores[$i]['score'];
					}
				}
				unset($expert);
			}
			$emploies = $this->User_model->find_employ_count($expert_string);
			for($i = 0, $n = count($emploies); $i < $n; $i++) {
				foreach($experts as &$expert) {
					if ($expert['created_by'] == $emploies[$i]['user_id']) {
						$expert['employ_count'] = $emploies[$i]['total'];
					}
				}
				unset($expert);
			}
			$verts = $this->User_model->find_vertification_info($expert_string);
			for($i = 0, $n = count($verts); $i < $n; $i++) {
				foreach($experts as &$expert) {
					if ($expert['created_by'] == $verts[$i]['created_by']) {
						if($verts[$i]['total']) {
							$expert['vertification'] = "是";
						} else {
							$expert['vertification'] = "否";
						}
					}
				}
				unset($expert);
			}
		}
		if (!empty($city_ids)) {
			$this->data['page_cities'] = $this->City_model->find_cities_by_ids($city_ids);
			$this->data['page_areas'] = $this->City_model->find_areas_by_ids($area_ids);
		}
		$this->data['search_type'] = 'expert';
		$this->data['experts'] = $experts;
		$this->data['pager'] =  $this->pager->create_links();
		$this->load->view('expert/lists', $this->data);
	}
	/**
	 * 删除行家
	 */
	public function delete() {
		$expert_id = intval($this->uri->segment(3));
		$expert = $this->Expert_model->find_by_id($expert_id);
		$current_user = $this->authorize->current_user();
		if (!empty($expert['created_by']) && $expert['created_by'] == $current_user) {
			$this->Expert_model->delete($expert_id);
			la_redirect("user/experts?user_id=" . $current_user, "行家删除成功");
		} else {
			la_redirect("", "你没有权利进行此操作");
		}
	}
}
